<template>
  <div class="table_header_btn_box">
    <div class="table_header_btn">
      <el-button type="text" class="btn_box" @click="otherClick('download')" :disabled="judgePermission('download')">
        <i class="icon-downloads1 iconfont-el icon"></i>
        <span class="name">{{ $t('teamDoc.rightBtnList.download') }} </span>
      </el-button>
      <el-button type="text" class="btn_box" @click="otherClick('delete')" :disabled="judgePermission('delete')">
        <svg-icon icon-class="trash" class="icon"></svg-icon>
        <span class="name">{{ $t('teamDoc.rightBtnList.invalid') }} </span>
      </el-button>
      <el-button v-if="tableRowArray.length == 1" type="text" class="btn_box" @click="otherClick('collection')">
        <svg-icon v-if="tableRowArray[0].collection == '1'" icon-class="collect-fill" class="icon_size"></svg-icon>
        <svg-icon v-else icon-class="collect" class="icon_size"></svg-icon>
        <span class="name">{{ $t('teamDoc.rightBtnList.collect') }} </span>
      </el-button>

      <!-- 企业多个文件添加标签的操作 -->
      <el-button v-if="tableRowArray.length != 1 && pageType == 'enterprise'" type="text" class="btn_box" @click="otherClick('enterpriseLabel')" :disabled="judgePermission('enterpriseLabel')">
        <i class="iconfont-el icon-collection icon"></i>
        <span class="name">{{ $t('teamDoc.rightBtnList.tagDocuments') }}</span>
      </el-button>

      <el-button v-if="tableRowArray.length == 1" type="text" class="btn_box" @click="otherClick('detail')">
        <i class="iconfont-el icon-view icon"></i>
        <span class="name">{{ $t('teamDoc.rightBtnList.details') }}</span>
      </el-button>

      <el-button v-if="tableRowArray.length == 1 && tableRowArray[0].type == 'file'" type="text" class="btn_box" @click="otherClick('history')">
        <svg-icon icon-class="history" class="icon" />
        <span class="name">{{ $t('teamDoc.rightBtnList.versions') }}</span>
      </el-button>
      <el-button
        v-if="tableRowArray.filter((item) => item.fileDatasetId == null).length == 0"
        type="text"
        class="btn_box"
        @click="otherClick('joinKnowledge')"
      >
        <i class="icon-refresh-left iconfont-el icon"></i>
        <span class="name">重新加入问答知识库</span>
      </el-button>

      <el-button
        v-if="!uploadKnowledgeBaseShow"
        type="text"
        class="btn_box"
        @click="otherClick('uploadkKnowledge')"
      >
        <i class="icon-upload iconfont-el icon"></i>
        <span class="name">上传问答知识库</span>
      </el-button>

      <!-- <el-button type="text" class="btn_box" @click="otherClick('exportBtn')" v-if="isFolderOnly">
        <i class="iconfont-el icon-upload2 icon"></i>
        <span class="name">{{ $t('teamDoc.rightBtnList.export') }}</span>
      </el-button> -->
      <el-button type="text" class="btn_box" @click="otherClick('saveAsTemplateLiabry')">
        <i class="iconfont-el icon-document-add icon"></i>
        <span class="name">保存为知识库模板</span>
      </el-button>

      

      <el-button v-if="isFileLibrary" type="text" class="btn_box" @click="otherClick('fileLibrary')">
        <i class="el-icon-upload2" />
        <span class="name">{{ $t('teamDoc.rightBtnList.fileIntoStorage') }}</span>
      </el-button>
      <!-- <div v-if="pageType == 'enterprise'" class="btn_box"> -->
      <div v-if="false" class="btn_box">
        <el-dropdown @command="fileEnhanceCommand" trigger="click">
          <!-- <el-button type="text" class="filter-item dropdownBtn" size="small" icon="el-icon-tickets" :disabled="tableRowArray.length == 0">文件增强</el-button> -->
          <div class="btn_box">
            <svg-icon icon-class="floder" class="icon" />
            <span class="name">{{ $t('teamDoc.rightBtnList.fileEnhance') }}</span>
          </div>
          <el-dropdown-menu slot="dropdown">
            <div class="dropdown_menu">
              <el-dropdown-item class="Dropdown_item_menu" icon="el-icon-notebook-2" command="life">{{
                $t('teamDoc.rightBtnList.life')
              }}</el-dropdown-item>
              <el-dropdown-item
                v-if="tableRowArray.length == 1 && pageType == 'enterprise' && tableRowArray[0].type == 'folder'"
                class="Dropdown_item_menu"
                command="createTemp"
              >
                <svg-icon icon-class="xiangmu1" class="icon_size icon_mr"></svg-icon>
                <span>{{ $t('teamDoc.rightBtnList.fileTemp') }}</span>
              </el-dropdown-item>
              <!-- v-if="$yuUtils.LicenseInfoFunc('yideng:list')" -->
              <div>
                <el-dropdown-item
                  v-if="tableRowArray.length == 1 && pageType == 'enterprise'"
                  class="Dropdown_item_menu"
                  icon="el-icon-coin"
                  command="metadata"
                >
                  {{ $t('teamDoc.rightBtnList.metadata') }}
                </el-dropdown-item>
              </div>
            </div>
          </el-dropdown-menu>
        </el-dropdown>

        <el-dropdown @command="moreListCommand" trigger="click">
          <div class="btn_box">
            <svg-icon icon-class="ai_chat3" class="icon_size icon_mr" />
            {{ $t('teamDoc.rightBtnList.aiFunction') }}
            <!-- <i class="el-icon-caret-right"></i> -->
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item class="Dropdown_item_menu">
                <svg-icon icon-class="gongzuozongjie" class="icon_size icon_mr" />
                <span>{{ $t('teamDoc.rightBtnList.aiSummarize') }}</span>
              </el-dropdown-item>
              <el-dropdown-item class="Dropdown_item_menu">
                <svg-icon icon-class="tiaocha" class="icon_size icon_mr" />
                <span>{{ $t('teamDoc.rightBtnList.aiAbstract') }}</span>
              </el-dropdown-item>
              <el-dropdown-item class="Dropdown_item_menu">
                <svg-icon icon-class="ai_chat3" class="icon_size icon_mr" />
                <span>{{ $t('teamDoc.rightBtnList.aiChat') }}</span>
              </el-dropdown-item>
              <el-dropdown-item class="Dropdown_item_menu item_border_bottom">
                <svg-icon icon-class="zhishichouqu" class="icon_size icon_mr" />
                <span>{{ $t('teamDoc.rightBtnList.aiExtract') }}</span>
              </el-dropdown-item>
              <el-dropdown-item class="Dropdown_item_menu item_border_bottom">
                <svg-icon icon-class="huiyi" class="icon_size icon_mr" />
                <span>{{ $t('teamDoc.rightBtnList.aiRead') }}</span>
              </el-dropdown-item>
              <el-dropdown-item class="Dropdown_item_menu">
                <svg-icon icon-class="xlsq-high-copy" class="icon_size icon_mr" />
                <span>{{ $t('teamDoc.rightBtnList.aiDrill') }}</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
      <!-- v-if="pageType == 'enterprise' && $store.state.settings.knowledge_base_state == '1'"  -->
      <el-button type="text" class="btn_box" :disabled="isSyncAi" @click="otherClick('syncKnowledge')" v-if="false">
        <!-- <i class="el-icon-refresh icon"></i> -->
        <svg-icon icon-class="xlsq-high-copy" class="icon_size icon_mr" />
        <span class="name">{{ $t('teamDoc.rightBtnList.aiDrill') }}</span>
      </el-button>
    </div>
  </div>
</template>

<script setup lang="ts" name="table-header-btn">
import { Plus, Link } from '@element-plus/icons-vue';
const { proxy } = getCurrentInstance() as any;
import { enterpriseRole } from '@/store/modules/enterpriseRole';
const roleStore = enterpriseRole();
const props = defineProps({
  pageType: {
    type: String,
    default: '' //enterprise:企业,team:团队,self:个人
  },

  tableRowArray: {
    //用于文件增强
    type: Array,
    default: () => {
      return [];
    }
  },
  rowData: {
    type: Object,
    default: () =>{}
  }
});
const tableRowArray: any = ref([]);
watch(
  () => props.tableRowArray,
  (newValue, oldValue) => {
    nextTick(()=>{
      getRoollist();
    })
    tableRowArray.value = newValue;
  },
  { immediate: true }
);
const roleList = ref<any[]>([]);
const getRoollist = () => {
  let paramsQuery = props.tableRowArray.find((item: any) => item.perm == 'query');
  let paramsEditor = props.tableRowArray.find((item: any) => item.perm == 'editor');
  if (paramsQuery) {
    roleStore.getEnteroriseFileRole(paramsQuery);
    roleList.value = roleStore.enterpriseMyRoleList;
  } else if (paramsEditor) {
    roleStore.getEnteroriseFileRole(paramsEditor);
    roleList.value = roleStore.enterpriseMyRoleList;
  } else {
    roleStore.getEnteroriseFileRole(tableRowArray.value[0]);
    roleList.value = roleStore.enterpriseMyRoleList;
  }
};
//同步到AI模型
const isSyncAi = computed(() => {
  if (props.pageType == 'enterprise') {
    // return !proxy.$yuUtils.documentsPermissionFunc(["syncAi"], this.$store.state.enterpriseRole.enterpriseMyRoleList);
  } else if (props.pageType == 'team') {
    return false;
  } else {
    return false;
  }
});
// 归档入库的显示条件
const isFileLibrary = computed(() => {
  let isShow = false;
  // let res = this.tableRowArray.filter((item) => item.type == "folder");
  // if (this.pageType == "enterprise" && this.tableRowArray.length > 0 && res.length == 0 && this.$yuUtils.LicenseInfoFunc("archives:list")) {
  //   isShow = true;
  // }
  return isShow;
});
// 检查 tableRowArray 中的每一个元素是否都是 'folder',不是就不显示
const isFolderOnly = computed(() => {
  return tableRowArray.value.every((item) => item.type === 'folder');
});
const emit = defineEmits(['otherClick', 'fileEnhanceCommand']);

const otherClick = (str) => {
  // 这个方法在globalMixins.js文件里
  emit('otherClick', str);
};
// 更多
const moreListCommand = (command: any) => {
  emit('otherClick', command);
};
//文件增强
const fileEnhanceCommand = (str: any) => {
  // 密级
  if (str == 'safety') {
    // this.dialogSafety = true;
  } else {
    emit('fileEnhanceCommand', str);
  }
};
// 内外摆渡按钮显示判断
const fileFerryHidden = () => {
  return false;
  // if (this.$store.state.user.ferrySwitch && this.pageType == "self" && this.$yuUtils.vifpermissionRoles(["admin", "ferry:list"])) {
  //   return !false;
  // } else {
  //   return !true;
  // }
};
// 模板库菜单显示判断
const isShowMenu = () => {
  if (
    (tableRowArray.value[0]?.type === 'file' && ['ppt', 'excel', 'docx'].includes(tableRowArray.value[0].suffix)) ||
    props.pageType === 'enterprise'
  ) {
    return true;
  } else {
    return false;
  }
};
/**权限控制 */
const judgePermission = (str) => {
  return !proxy.$yuUtils.documentsPermissionFunc([str], roleList.value);
};
// 上传知识库显示条件
const uploadKnowledgeBaseShow = computed(() => {
    let show:boolean = false;
    if(props.rowData.aiDatasetId){
      if(tableRowArray.value.filter(item => item.type == 'folder').length == 0 && tableRowArray.value.filter((item) => item.fileDatasetId != null).length == 0){
        if(tableRowArray.value.filter(item => item.suffix != 'docx' && item.suffix != 'txt' && item.suffix != 'markdown' && item.suffix != 'pdf' && item.suffix != 'html' && item.suffix != 'xlsx' && item.suffix != 'xls' && item.suffix != 'csv' && item.suffix != 'md' && item.suffix != 'htm').length == 0){
          show = false;
        }else{
          show = true;
        }
      }else{
        show = true;
      }
    }else{
      show = true;
    }
    return show;
})
</script>
<style scoped lang="scss">
.table_header_btn_box {
  // height: 34px;
}

.table_header_btn {
  height: 34px;
  display: flex;
  align-items: center;
  position: relative;
  z-index: 800;
  padding: 0;
}

.btn_box {
  display: flex !important;
  align-items: center !important;
  color: #666;
  font-size: 14px;
  margin-right: 6px;
  cursor: pointer;
  padding: 0;
  line-height: normal;

  .icon {
    color: #999;
    font-size: 15px;
    margin-top: 2px;
  }

  .name {
    margin-left: 6px;
  }

  &:hover {
    color: #6698ff;

    .icon {
      color: #6698ff;
    }
  }
}

.el-button.is-disabled.el-button--text {
  color: #c0c4cc;
  cursor: not-allowed;

  .icon {
    color: #c0c4cc;
  }

  &:hover {
    color: #c0c4cc;

    .icon {
      color: #c0c4cc;
    }
  }
}

.Dropdown_item_menu {
  padding-top: 5px !important;
  padding-bottom: 5px !important;
  font-size: 14px !important;

  &:hover {
    background-color: #f5f5f5 !important;
    color: #606266;
  }
}

.Dropdown_item_menu_more {
  position: relative;
  padding-top: 5px !important;
  padding-bottom: 5px !important;
  font-size: 14px !important;

  .info {
    display: none;
  }

  &:hover {
    .info {
      display: block;
      position: absolute;
      left: 120px;
      top: -50%;
      width: 100px;
      height: 100px;
      background-color: white;
      padding: 10px 0px;

      p {
        margin: 0;
        padding: 0;
        text-align: center;
        line-height: 35px;

        &:hover {
          background-color: #f5f5f5 !important;
          color: #606266;
        }
      }
    }

    background-color: #f5f5f5 !important;
    color: #606266;
  }
}

.item_border_bottom {
  border-bottom: 1px solid #eee;
}

.icon_mr {
  margin-right: 6px;
}
</style>